/**
****************************************************************
* @file iso15693.c
*
* @brief  关于15693协议一些基本的底层驱动接口
*
* @author 
*
* 
****************************************************************
*/ 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "iso15693.h"
#include "nfc663.h"
#include "define.h"
#include "rfid.h"
#include "timer.h"

int pcd_send_eof(u8 *pRxBuffer, u16 *pRxLength)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;	
	
#if (NFC_DEBUG)
		printf("SEND EOF:\n");
#endif
	pcd_set_15693_tmo(8);

	write_reg(0x37, 0x55);
	set_bit_mask(0x31,BIT4);
	write_reg(0x37, 0x00);
	
	write_reg(CommandReg, PCD_TRANSMIT);
	set_bit_mask(BitFramingReg,0x80);
	
	mf_com_data.mf_command = PCD_RECEIVE;
	status = pcd_com_transceive(pi);
	
	write_reg(0x37, 0x55);
	clear_bit_mask(0x31,BIT4);
	write_reg(0x37, 0x00); 
	
	if (status != MI_OK)
	{
		return status;
	}

	*pRxLength = (mf_com_data.mf_length / 8);
	memcpy(pRxBuffer, mf_com_data.mf_data, *pRxLength);

	return MI_OK;	
}

int pcd_inventory(
				  u8 bFlag, 
				  u8  bAfi, 
				  u8 *pMask, 
				  u8  bMaskBitLength, 
				  u8 *pDsfid, 
				  u8 *pUid)
{
	int XDATA status = MI_OK;
	static u8 XDATA bit_rand = 0;
	u8 XDATA bit_index = 0;
	u8 XDATA byte_index = 0;
	u8 XDATA last_bit = 0;
	transceive_buffer XDATA *pi;
	
	pi = &mf_com_data;
		
#if (NFC_DEBUG)
	printf("INVENTORY:\n");
#endif	
	pcd_set_15693_tmo(4);

	do
	{
		mf_com_data.mf_command = PCD_TRANSCEIVE;
		mf_com_data.mf_data[0] = bFlag;	//Data_rate_flag | Inventory_flag | Nb_slots_flag;;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
		mf_com_data.mf_data[1] = INVENTORY_CMD;
		if((bFlag & BIT2) && (bFlag & BIT4))		//AFI_Flag = 1
		{
			mf_com_data.mf_data[2] = bAfi;
			mf_com_data.mf_data[3] = bMaskBitLength;

			
			if(bMaskBitLength == 0)
			{
				mf_com_data.mf_length = 4;
			}
			else
			{
				memcpy(&mf_com_data.mf_data[4], pMask, (mask_byte_number(bMaskBitLength) + 1));
				mf_com_data.mf_length = 4 + mask_byte_number(bMaskBitLength) + 1;	
			}
		}
		else
		{
			mf_com_data.mf_data[2] = bMaskBitLength;

			if(bMaskBitLength == 0)
			{
				mf_com_data.mf_length = 3;
			}
			else
			{
				memcpy(&mf_com_data.mf_data[3], pMask, (mask_byte_number(bMaskBitLength) + 1));
				mf_com_data.mf_length = 3 + mask_byte_number(bMaskBitLength) + 1;	
			}
		}
		
		status = pcd_com_transceive(pi);
		if (status != MI_OK)
		{
			if(status == MI_COLLERR)
			{
				mf_com_data.mf_data[0] = mf_com_data.mf_data[0] + 1;		//涓轰ュｅ煎
				bit_rand += 1;
				/* The first two bytes coll */
				if(mf_com_data.mf_data[0] <= 0x10)
				{
					bMaskBitLength = 0;
					break;
				}
				bMaskBitLength = mf_com_data.mf_data[0] - 16;	//下次的mask长度，16为FLAG和DSFID长度
				bit_index = mask_bit_number(bMaskBitLength); 	//冲突的bit
				byte_index = mask_byte_number(bMaskBitLength);	//冲突bit处于maskval中的位置
				memcpy(pMask, &mf_com_data.mf_data[3], byte_index + 1); //前面未冲突的字节部分 + 1

				pMask[byte_index] = bit_index * bit_random(bit_rand);	//冲突bit交替变换
				  
				for(bit_index = 0; bit_index <= ((bMaskBitLength - 1) % 8); bit_index++)
				{
					last_bit |= BIT(bit_index);
				}

				pMask[byte_index] |= (last_bit & mf_com_data.mf_data[3]); //将冲突位以及冲突前的位进行合并
				continue;

			}
			
			return status;
		}
		
		if((mf_com_data.mf_length / 8) != ISO15693_INVENTORY_RSP_LEN)
		{ 
			return PROTOCOL_ERR;
		}	
	}while(status == MI_COLLERR);

	*pDsfid = mf_com_data.mf_data[1];
	memcpy(pUid, &mf_com_data.mf_data[2], ISO15693_UID_LENGTH);

	return MI_OK;	
}

int pcd_stay_quiet(u8 *pUid, u8 bFlag)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;	
	
#if (NFC_DEBUG)
		printf("STAY_QUIET:\n");
#endif
	pcd_set_15693_tmo(0);
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;	//Data_rate_flag | Address_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = STAY_QUIET_CMD;
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	
	status = pcd_com_transceive(pi);

	if(status == MI_NOTAGERR)
	{
		return MI_OK;
	}

	return PROTOCOL_ERR;

}

int pcd_reset_to_ready(u8 *pUid, u8 bFlag)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;	
	
#if (NFC_DEBUG)
		printf("RESET TO READY:\n");
#endif
	pcd_set_15693_tmo(0);
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;	//Data_rate_flag | Address_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = RESET_TO_READY_CMD;
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	
	status = pcd_com_transceive(pi);

	if(status == MI_NOTAGERR)
	{
		return MI_OK;
	}

	return PROTOCOL_ERR;	
}

int pcd_read_single_block(
					 u8  *pUid,
					 u8   bFlag, 
					 u8   bBlockNum,  
					 u8  *pRxBuffer, 
					 u8  *pErr,
					 u16 *pRxLength
					 )
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("READ SINGLE BLOCK:\n");
#endif	
	pcd_set_15693_tmo(4);
	
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 11;
	mf_com_data.mf_data[0] = bFlag;//Data_rate_flag | Address_flag | Option_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = READ_SINGLE_BLOCK_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum;
	status = pcd_com_transceive(pi);

	if (status != MI_OK)
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pRxLength = 0;
		*pErr = mf_com_data.mf_data[1];
		return status;
	}
	else if(!(bFlag & OPTION_FLAG))
	{
		*pRxLength = (mf_com_data.mf_length / 8) - 1;
		memcpy(pRxBuffer, &mf_com_data.mf_data[1], *pRxLength);
	}
	else		//buffer第一个字节是安全状态
	{
		*pRxLength = (mf_com_data.mf_length / 8) - 1;
		memcpy(pRxBuffer, &mf_com_data.mf_data[1], *pRxLength);
	}

	return MI_OK;	
}

int pcd_write_single_block(
							  u8  *pUid,
							  u8   bFlag, 
							  u8   bBlockNum, 
							  u8  *pTxBuffer,
							  u8  *pErr,
							  u16  wTxLength
							  )
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("WRITE SINGLE BLOCK:\n");
#endif	
	pcd_set_15693_tmo(8);
	
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = (11 + wTxLength);
	mf_com_data.mf_data[0] = bFlag;//Data_rate_flag | Address_flag | Option_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = WRITE_SINGLE_BLOCK_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum;
	memcpy(&mf_com_data.mf_data[11], pTxBuffer, wTxLength);

	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	
}

int pcd_lock_block(u8 *pUid, u8 bFlag, u8 bBlockNum, u8 *pErr)

{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("LOCK BLOCK:\n");
#endif		
	pcd_set_15693_tmo(8);

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 11;
	mf_com_data.mf_data[0] = bFlag;
	mf_com_data.mf_data[1] = LOCK_BLOCK_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum;	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	
}

int pcd_read_multiple_block(
							u8  *pUid, 
							u8  bFlag, 
							u8  bBlockNum, 
							u8  bNumBlocks, 
							u8  *pRxBuffer, 
							u8  *pErr, 
							u16 *pRxLength
							)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("READ MULTIPLE BLOCK:\n");
#endif		
	pcd_set_15693_tmo(12);	

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 12;
	mf_com_data.mf_data[0] = bFlag;	
    mf_com_data.mf_data[1] = READ_MULTIPLE_BLOCK_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum; 
	mf_com_data.mf_data[11] = bNumBlocks;		//eg:bNumBlocks = 0x06其实是读7个块
	
	status = pcd_com_transceive(pi);
	
	if (status != MI_OK)		
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) 		//假设一个块是4个Byte	
		&& (mf_com_data.mf_length / 8 != (5 * (1 + bNumBlocks)))
		&& (mf_com_data.mf_length / 8 != (4 * (1 + bNumBlocks))))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	else if((mf_com_data.mf_length / 8 != (4 * (1 + bNumBlocks))) && (!(bFlag & OPTION_FLAG)))
	{
		*pRxLength = (mf_com_data.mf_length / 8) - 1;
		memcpy(pRxBuffer, &mf_com_data.mf_data[1], *pRxLength);
	}
	else if((mf_com_data.mf_length / 8 != (5 * (1 + bNumBlocks))) && (!(bFlag & OPTION_FLAG)))
	{
		*pRxLength = (mf_com_data.mf_length / 8) - 1;
		memcpy(pRxBuffer, &mf_com_data.mf_data[1], *pRxLength);
	}
	else
	{ 
		return PROTOCOL_ERR;
	}

	return MI_OK;	
}

int pcd_write_multiple_block(
						 u8  *pUid,
						 u8   bFlag, 
						 u8   bBlockNum,
						 u8   bNumBlocks,
						 u8  *pTxBuffer,
						 u8  *pErr,
						 u16  wTxLength
						 )
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("WRITE MULTIPLE BLOCK:\n");
#endif	
	pcd_set_15693_tmo(8);
	
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = (12 + wTxLength);
	mf_com_data.mf_data[0] = bFlag;//Data_rate_flag | Address_flag | Option_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = WRITE_MULTIPLE_BLOCK_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum;
	mf_com_data.mf_data[11] = bNumBlocks;
	memcpy(&mf_com_data.mf_data[12], pTxBuffer, wTxLength);

	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	
}

int pcd_select(u8 *pUid, u8 bFlag, u8 *pErr)
{	
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("SELECT:\n");
#endif	
	pcd_set_15693_tmo(3);	

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = SELECT_CMD;	//cmd：	查找所以处于RF场内的VICC
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	
	status = pcd_com_transceive(pi);
	if(status != MI_OK)
	{
		return status;
	}	
	
	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;		
}

int pcd_write_afi(u8 *pUid, u8 bFlag, u8 bAfi, u8 *pErr)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("WRITE AFI:\n");
#endif		
	pcd_set_15693_tmo(8);

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 11;
	mf_com_data.mf_data[0] = bFlag;
	mf_com_data.mf_data[1] = WRITE_AFI_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bAfi;	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	

}

int pcd_lock_afi(u8 *pUid, u8 bFlag, u8 *pErr)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("LOCK BLOCK:\n");
#endif		
	pcd_set_15693_tmo(8);

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;
	mf_com_data.mf_data[1] = LOCK_AFI_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	
}

int pcd_write_dsfid(u8 *pUid, u8 bFlag, u8 bDsfid, u8 *pErr)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("WRITE AFI:\n");
#endif		
	pcd_set_15693_tmo(8);

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 11;
	mf_com_data.mf_data[0] = bFlag;
	mf_com_data.mf_data[1] = WRITE_DSFID_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bDsfid;	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	

}


int pcd_lock_dsfid(u8 *pUid, u8 bFlag, u8 *pErr)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("LOCK BLOCK:\n");
#endif		
	pcd_set_15693_tmo(8);

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;
	mf_com_data.mf_data[1] = LOCK_DSFID_CMD;	
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != 1))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	return MI_OK;	
}

int pcd_get_system_information(
						u8  *pUid, 
						u8   bFlag, 
						u8  *pErr, 
						u8  *pRxBuffer, 
						u16 *pRxLength
						)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("GET SYSTEM INFORMATION:\n");
#endif		
	pcd_set_15693_tmo(8);	

	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 10;
	mf_com_data.mf_data[0] = bFlag;//Data_rate_flag | Address_flag;	//标志：单副载波、高速率、flag[8:5] 按照表5( 1 slot )
    mf_com_data.mf_data[1] = GET_SYSTEM_INFORMATION_CMD;	//cmd：	查找所以处于RF场内的VICC
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);	
	
	status = pcd_com_transceive(pi);

	if (status != MI_OK)		
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pRxLength = 0;
		*pErr = mf_com_data.mf_data[1];
		return status;
		
	}
	*pRxLength = (mf_com_data.mf_length / 8);
	memcpy(pRxBuffer, &mf_com_data.mf_data[0], *pRxLength);
	return MI_OK;		

	
}

int pcd_get_multiple_block_security_status(
									u8  *pUid,
									u8   bFlag, 
									u8   bBlockNum,
									u8   bNumBlocks,
									u8  *pErr,
									u8  *pSecurityStatus, 
									u16 *pSecStaLength
									)
{
	int XDATA status = MI_OK;
	transceive_buffer XDATA *pi;

	pi = &mf_com_data;

#if (NFC_DEBUG)
	printf("GET MULTTIPLE BLOCK SECURITY STATUS:\n");
#endif	
	pcd_set_15693_tmo(8);
	
	mf_com_data.mf_command = PCD_TRANSCEIVE;
	mf_com_data.mf_length = 12;
	mf_com_data.mf_data[0] = bFlag;
	memcpy(&mf_com_data.mf_data[2], pUid, ISO15693_UID_LENGTH);
	mf_com_data.mf_data[10] = bBlockNum;
	mf_com_data.mf_data[11] = bNumBlocks;

	status = pcd_com_transceive(pi);

	if (status != MI_OK)		//return以后根据option_flag决定是否要发送EOF从而接收数据
	{
		return status;
	}

	if((mf_com_data.mf_length / 8 != 2) && (mf_com_data.mf_length / 8 != (bNumBlocks + 1)))
	{ 
		return PROTOCOL_ERR;
	}	

	if((mf_com_data.mf_length / 8 == 2) && (mf_com_data.mf_data[0] & ERROR_FLAG))
	{
		*pErr = mf_com_data.mf_data[1];
	}
	
	*pSecStaLength = (mf_com_data.mf_length / 8) - 1;
	memcpy(pSecurityStatus, &mf_com_data.mf_data[1], *pSecStaLength);
	return MI_OK;		
}

void pcd_set_15693_tmo(u8 time)
{
	write_reg(TPrescalerReg, (2048) & 0xFF);
	write_reg(TModeReg, BIT7 | (((2048)>>8) & 0xFF));

	write_reg(TReloadRegL, (1 << time) & 0xFF);
	write_reg(TReloadRegH, ((1 << time) & 0xFF00) >> 8);
}
